// 请求接口的集合
import { getModel, positionMsg } from "@/api/model";
import { putOssTempUrl } from "@/api/oss";
import { editOrAddPoint, getPoint } from "@/api/map";
import { getEnums } from "@/api/public";
import { panoramaList } from "@/api/pannellum";

export function initApi(vm) {
  return {
    requestpanoramaList() {
      let queryParams = {
        page: {
          pages: 1,
          size: 65535,
        },
      }
      queryParams.floorId = vm.mapId;
      vm.vrPanoramaArr = [];
      return new Promise((resolve, reject) => {
        panoramaList(queryParams).then((res) => {
          if (res.success) {
            let arr = res?.data ?? [];
            if (arr.length > 0) {
              arr.forEach((item) => {
                let labelValue = item.mapName + "-" + item.title;
                let obj = {
                  label: labelValue,
                  // value: item.encResID,
                  value: item.sceneId,
                }
                vm.vrPanoramaArr.push(obj)
              })
            }
            vm.$refs.targetChose.getvrPanoramaArr(vm.vrPanoramaArr)
            resolve(true)
          } else {
            reject(false)
          }
        }).catch((err) => {
          reject(false)
        })
      });
    },
    // 请求枚举列表获取点位类型
    requestEnums() {
      vm.positionType = [];
      return new Promise((resolve, reject) => {
        getEnums()
          .then((res) => {
            const obj = res?.data?.PositionType ?? {};
            if (Object.keys(obj).length > 0) {
              for (const key in obj) {
                vm.positionType.push({
                  label: obj[key],
                  value: key,
                });
              }
              resolve(true);
            } else {
              resolve(true);
            }
          })
          .catch((err) => {
            reject(false);
          });
      });
    },
    //! 请求楼层的点位信息
    requestFloorPoint() {
      vm.floorPoint = [];
      return new Promise((resolve, reject) => {
        getPoint(vm.mapId)
          .then((res) => {
            if (res.success) {
              vm.floorPoint = res?.data ?? [];
              resolve(vm.floorPoint);
            } else {
              reject(false);
            }
          })
          .catch((err) => {
            reject(false);
          });
      });
    },
    //! 保存点位信息
    requesteditOrAddPoint(arr, flage) {
      vm.$refs.messageBox.close();
      editOrAddPoint(vm.mapId, arr, flage)
        .then((res) => {
          setTimeout(() => {
            vm.saveLoad = false;
          }, 500);
          if (res.success) {
            vm.$message({
              message: "保存成功！",
              type: "success",
            });
            setTimeout(() => {
              vm.mapLoading = true;
              vm.loadLowLarDa();
              vm.requestpositionMsg();
            }, 1000);
          } else {
            vm.$message({
              message: "保存失败！",
              type: "error",
            });
          }
        })
        .catch((err) => {
          setTimeout(() => {
            vm.saveLoad = false;
          }, 500);
          vm.$message({
            message: "保存失败！",
            type: "error",
          });
        });
    },
    // 获取楼层的底图
    requestGetFloorImg() {
      vm.backimg = "";
      return new Promise((resolve, reject) => {
        putOssTempUrl(vm.ossId)
          .then((res) => {
            if (res.success) {
              vm.backimg = res?.data?.url ?? "";
              resolve(true);
            } else {
              reject(false);
            }
          })
          .catch((err) => {
            reject(false);
          });
      });
    },
    // 获取图片模型集合
    requestgetModel(params, falge) {
      return new Promise((resolve, reject) => {
        let query = {};
        if (params) {
          query = {
            ...params,
          };
        }
        getModel(query)
          .then((res) => {
            if (res.success) {
              const arr = res?.data ?? [];
              if (falge == "model") {
                vm.imgArr = arr;
              } else if (falge == "business") {
                vm.yewuimgArr = arr;
              } else if (falge === undefined) {
                vm.imgArr = arr;
                vm.yewuimgArr = arr;
                vm.listimgArr = arr;
              }
              resolve(true);
            } else {
              reject(false);
            }
          })
          .catch((err) => {
            reject(false);
          });
      });
    },
    // 获取业务列表
    requestpositionMsg(params) {
      const query = {
        unconfirmed: true,
        page: {
          pages: 1,
          size: 65535,
        },
      };
      if (params && params.preferType) {
        query.types = params.preferType;
      }
      if (params && params.name) {
        query.name = params.name;
      }
      positionMsg(query)
        .then((res) => {
          if (res.success) {
            const arr = res?.data ?? [];
            vm.positionMsgArr = arr;
            // vm.filteredPositionMsgArr = vm.positionMsgArr;
            vm.filterPositionMsgArr()
          }
        })
        .catch((err) => { });
    },
  };
}
